﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

namespace He.Web.manage.adminlogs
{
    public partial class list : He.Web.UI.Manage
    {
        protected int psize = 12;                          //每页记录数
        protected int page;                                   //当前页
        protected int pcount;                                 //记录总数
        protected string add_time;                            //属性
        protected string keywords;                            //关键字
        protected void Page_Load(object sender, EventArgs e)
        {
            page = GetInt("page", 0);
            add_time = GetString("add_time");
            keywords = GetString("keywords");

            if (!this.Page.IsPostBack)
            {
                chkLoginLevel("viewAdminlogs");
                RptBind();
            }
        }

        #region 数据绑定
        private void RptBind()
        {
            this.txtAddTime.Text = this.add_time;
            this.txtKeywords.Text = this.keywords;

            string strWhere = Comb_SqlTxt(this.add_time, this.keywords);
            He.BLL.adminlogs bll = new BLL.adminlogs();
            rptList.DataSource = bll.GetPageList(psize, page, strWhere, "id desc");
            rptList.DataBind();
            pcount = bll.GetRecordCount(strWhere);
        }
        #endregion

        #region  组合SQL语句
        private string Comb_SqlTxt(string _add_time, string _keywords)
        {
            StringBuilder strTemp = new StringBuilder();
            strTemp.Append(" id>0 ");
            if (!string.IsNullOrEmpty(_add_time))
            {
                strTemp.AppendFormat(" and add_time like '%{0}%'", _add_time);
            }
            if (!string.IsNullOrEmpty(_keywords))
            {
                strTemp.AppendFormat(" and admin_name like '%{0}%' or note like '%{0}%'", _keywords);
            }
            return strTemp.ToString();
        }
        #endregion

        #region 组合URL语句
        public string Comb_UrlTxt(string _add_time, string _keywords)
        {
            StringBuilder strTemp = new StringBuilder();
            if (!string.IsNullOrEmpty(_add_time))
            {
                strTemp.AppendFormat("&add_time={0}", _add_time);
            }
            if (!string.IsNullOrEmpty(_keywords))
            {
                strTemp.AppendFormat("&keywords={0}", _keywords);
            }
            return strTemp.ToString();
        }
        #endregion

        protected void lbtnDel_Click(object sender, EventArgs e)
        {
            chkLoginLevel("delAdminlogs");

            BLL.adminlogs bll = new BLL.adminlogs();
            for (int i = 0; i < rptList.Items.Count; i++)
            {
                CheckBox cb = (CheckBox)rptList.Items[i].FindControl("Check");
                int id = int.Parse(((HiddenField)rptList.Items[i].FindControl("HiddenId")).Value);
                if (cb.Checked)
                {
                    Model.adminlogs model = bll.GetModel(id);
                    bll.Delete(id);
                }
            }
            SaveLogs("[管理员操作日志管理]批量删除");
            ShowSuccess("批量删除成功啦！", Request.RawUrl);
        }

        protected void btnSelect_Click(object sender, EventArgs e)
        {
            Response.Redirect("list.aspx?page=0" + Comb_UrlTxt(this.add_time, txtKeywords.Text.Trim()));
        }

        protected void txtAddTime_TextChanged(object sender, EventArgs e)
        {
            Response.Redirect("list.aspx?page=0" + Comb_UrlTxt(this.txtAddTime.Text.Trim(), keywords));
        }
    }
}